package com.longge;

import java.util.Arrays;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-05-06 00:38
 */
public class NumTilePossibilities {

    int count = 0;

    public int numTilePossibilities(String tiles) {

        char[] chars = tiles.toCharArray();
        Arrays.sort(chars);
        boolean[] visited = new boolean[chars.length];
        backtrack(chars, visited);
        return count;
    }

    private void backtrack(char[] chars, boolean[] visited) {


        for (int i = 0; i < chars.length; i++) {
            if (visited[i] || (i > 0 && chars[i] == chars[i - 1] && !visited[i - 1])) continue;
            visited[i] = true;
            count++;
            backtrack(chars, visited);
            visited[i] = false;
        }
    }
}
